package com.mojian.controller.site;

import java.util.List;
import java.util.Map;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.mojian.entity.SysResource;
import com.mojian.service.SysResourceService;
import com.mojian.common.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;

/**
 * 用户资源审核 控制器
 */
@RestController
@RequestMapping("/sys/user-resource")
@RequiredArgsConstructor
@Api(tags = "用户资源审核管理")
public class SysUserResourceController {

    private final SysResourceService sysResourceService;

    @GetMapping("/list")
    @ApiOperation(value = "获取待审核用户资源列表")
    public Result<IPage<SysResource>> list(SysResource sysResource) {
        // 设置状态为待审核
        if (sysResource.getStatus() == null) {
            sysResource.setStatus(1); // 默认查询待审核的资源
        }
        return Result.success(sysResourceService.selectPage(sysResource));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "获取用户资源详情")
    public Result<SysResource> getInfo(@PathVariable("id") Long id) {
        return Result.success(sysResourceService.getById(id));
    }

    @PostMapping("/review")
    @ApiOperation(value = "审核用户资源")
    public Result<Object> review(@RequestBody Map<String, Object> params) {
        Long id = Long.parseLong(params.get("id").toString());
        Integer status = Integer.parseInt(params.get("status").toString());
        String rejectReason = params.get("rejectReason") != null ? params.get("rejectReason").toString() : null;
        
        SysResource resource = sysResourceService.getById(id);
        if (resource == null) {
            return Result.error("资源不存在");
        }
        
        resource.setStatus(status);
        // 如果有拒绝原因，可以保存到资源的描述字段或其他字段
        if (rejectReason != null && status == 2) {
            // 在实际应用中，您可能需要扩展SysResource实体类，添加rejectReason字段
            // 或者创建一个审核记录表来保存拒绝原因
            resource.setDescription(resource.getDescription() + "\n【审核拒绝原因】：" + rejectReason);
        }
        
        return Result.success(sysResourceService.update(resource));
    }

    @PostMapping("/batch-review")
    @SaCheckPermission("sys:user-resource:approve")
    @ApiOperation(value = "批量审核用户资源")
    public Result<Object> batchReview(@RequestBody Map<String, Object> params) {
        List<Long> ids = (List<Long>) params.get("ids");
        Integer status = Integer.parseInt(params.get("status").toString());
        String rejectReason = params.get("rejectReason") != null ? params.get("rejectReason").toString() : null;
        
        boolean success = true;
        for (Long id : ids) {
            SysResource resource = sysResourceService.getById(id);
            if (resource != null) {
                resource.setStatus(status);
                // 如果有拒绝原因，可以保存到资源的描述字段或其他字段
                if (rejectReason != null && status == 2) {
                    resource.setDescription(resource.getDescription() + "\n【审核拒绝原因】：" + rejectReason);
                }
                
                success = success && sysResourceService.update(resource);
            }
        }
        
        return Result.success(success);
    }

    @DeleteMapping("/delete/{ids}")
    @SaCheckPermission("sys:user-resource:delete")
    @ApiOperation(value = "删除用户资源")
    public Result<Object> remove(@PathVariable List<Long> ids) {
        return Result.success(sysResourceService.deleteByIds(ids));
    }
} 